package com.shenke.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenke.user.common.BaseResponse;
import com.shenke.user.common.ErrorCode;
import com.shenke.user.common.ResultUtils;
import com.shenke.user.exception.BusinessException;
import com.shenke.user.model.dto.UserLoginRequest;
import com.shenke.user.model.dto.UserRegisterRequest;
import com.shenke.user.model.dto.UserUpdateRequest;
import com.shenke.user.model.vo.UserVO;
import com.shenke.user.service.UserService;
import com.shenke.user.utils.NetUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/user/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request) {
        // 校验参数
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        String username = userRegisterRequest.getUsername();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(username, password, checkPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        // 获取 IP 地址
        String ipAddress = NetUtils.getIpAddress(request);
        long userId = userService.userRegister(userRegisterRequest, ipAddress);
        return ResultUtils.success(userId);
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @PostMapping("/user/login")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        // 校验参数
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        String phone = userLoginRequest.getPhone();
        String password = userLoginRequest.getPassword();
        if (StringUtils.isBlank(phone)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "手机号不能为空");
        }
        if (StringUtils.isBlank(password)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不能为空");
        }
        // 获取 IP 地址
        String ipAddress = NetUtils.getIpAddress(request);
        UserVO userVO = userService.userLogin(ipAddress, phone, password);
        return ResultUtils.success(userVO);
    }

    /**
     * 分页用户列表
     * @return
     */
    @GetMapping("/users")
    public BaseResponse<Page<UserVO>> listUserVOByPage() {
        // 获得当前登录用户 id
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        Page<UserVO> userVOPage = userService.listUserVOByPage(loginId);
        return ResultUtils.success(userVOPage);
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/user/{userId}")
    public BaseResponse<UserVO> getUserVOById(@PathVariable Long userId) {
        // 校验参数
        if (userId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        if (userId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        // 获得当前登录用户 id
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        UserVO userVO = userService.getUserVOById(userId, loginId);
        return ResultUtils.success(userVO);
    }

    /**
     * 修改用户信息
     * @param userId
     * @return
     */
    @PutMapping("/user/{userId}")
    public BaseResponse<UserVO> updateUser(@PathVariable Long userId, @RequestBody UserUpdateRequest userUpdateRequest) {
        // 校验参数
        if (userId == null || userUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不能为空");
        }
        String username = userUpdateRequest.getUsername();
        String email = userUpdateRequest.getEmail();
        String phone = userUpdateRequest.getPhone();
        UserVO userVO = userService.updateUser(userId, username, email, phone);
        return ResultUtils.success(userVO);
    }

    /**
     * 用户重置密码
     * @return
     */
    @PostMapping("/user/reset-password")
    public BaseResponse<List<Long>> userResetPwd() {
        // 获得当前登录用户 id
        Long loginId = Long.parseLong(StpUtil.getLoginId().toString());
        List<Long> failedList  = userService.userResetPwd(loginId);
        return ResultUtils.success(failedList);
    }
}
